package com.huawei.hms.update.manager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.update.ui.UpdateBean;
import com.huawei.hms.update.ui.a;
import com.huawei.hms.utils.PackageManagerHelper;
import com.huawei.hms.utils.PackageManagerHelper.PackageStates;
import com.huawei.hms.utils.ResourceLoaderUtil;
import com.huawei.hms.utils.Util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class UpdateManager {
    private static boolean a(Context context, String str) {
        PackageStates packageStates = new PackageManagerHelper(context).getPackageStates(str);
        HMSLog.i("UpdateManager", "app is: " + str + ";status is:" + packageStates);
        return PackageStates.ENABLED == packageStates;
    }

    private static boolean a(Context context) {
        return a(context, "com.android.vending") && b(context);
    }

    private static boolean b(Context context) {
        ResourceLoaderUtil.setmContext(context);
        boolean a = new a().a(1000, TimeUnit.MILLISECONDS, ResourceLoaderUtil.getString("hms_base_google"));
        HMSLog.i("UpdateManager", "ping google status is:" + a);
        return a;
    }

    public static void startUpdate(Activity activity, int i, UpdateBean updateBean) {
        Intent startUpdateIntent = getStartUpdateIntent(activity, updateBean);
        if (startUpdateIntent != null) {
            activity.startActivityForResult(startUpdateIntent, i);
        }
    }

    public static Intent getStartUpdateIntent(Activity activity, UpdateBean updateBean) {
        if (activity == null || updateBean == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        if (TextUtils.isEmpty(updateBean.getClientAppName())) {
            b(activity, arrayList);
        } else {
            a(activity, arrayList);
        }
        updateBean.setTypeList(arrayList);
        Intent intentStartBridgeActivity = BridgeActivity.getIntentStartBridgeActivity(activity, a.a(((Integer) arrayList.get(0)).intValue()));
        intentStartBridgeActivity.putExtra(BridgeActivity.EXTRA_DELEGATE_UPDATE_INFO, updateBean);
        return intentStartBridgeActivity;
    }

    public static Intent startUpdateIntent(Activity activity) {
        if (activity == null) {
            return null;
        }
        Intent intentStartBridgeActivity;
        Serializable updateBean = new UpdateBean();
        updateBean.setHmsOrApkUpgrade(true);
        updateBean.setClientPackageName(com.huawei.hms.utils.a.a(activity.getApplicationContext()).b());
        updateBean.setClientVersionCode(HuaweiApiAvailability.getServicesVersionCode());
        updateBean.setClientAppId(HuaweiApiAvailability.APPID_HMS);
        updateBean.setNeedConfirm(false);
        if (ResourceLoaderUtil.getmContext() == null) {
            ResourceLoaderUtil.setmContext(activity.getApplicationContext());
        }
        updateBean.setClientAppName(ResourceLoaderUtil.getString("hms_update_title"));
        ArrayList arrayList = new ArrayList();
        if (TextUtils.isEmpty(updateBean.getClientAppName())) {
            b(activity, arrayList);
        } else {
            a(activity, arrayList);
        }
        updateBean.setTypeList(arrayList);
        if (arrayList.size() > 1) {
            intentStartBridgeActivity = BridgeActivity.getIntentStartBridgeActivity(activity, a.a(((Integer) arrayList.get(1)).intValue()));
        } else {
            intentStartBridgeActivity = BridgeActivity.getIntentStartBridgeActivity(activity, a.a(((Integer) arrayList.get(0)).intValue()));
        }
        intentStartBridgeActivity.putExtra(BridgeActivity.EXTRA_DELEGATE_UPDATE_INFO, updateBean);
        return intentStartBridgeActivity;
    }

    private static void a(Activity activity, ArrayList<Integer> arrayList) {
        if (Util.isEMUI() && Util.isChinaROM()) {
            if (a((Context) activity, "com.huawei.appmarket") && c(activity)) {
                arrayList.add(Integer.valueOf(0));
                arrayList.add(Integer.valueOf(6));
                return;
            }
            arrayList.add(Integer.valueOf(6));
        } else if (a((Context) activity, "com.huawei.appmarket") && !"com.huawei.appmarket".equals(activity.getPackageName())) {
            arrayList.add(Integer.valueOf(5));
        } else if (a(activity)) {
            arrayList.add(Integer.valueOf(2));
        } else {
            arrayList.add(Integer.valueOf(6));
        }
    }

    private static void b(Activity activity, ArrayList<Integer> arrayList) {
        if (!a((Context) activity, "com.huawei.appmarket") || "com.huawei.appmarket".equals(activity.getPackageName())) {
            arrayList.add(Integer.valueOf(4));
        } else {
            arrayList.add(Integer.valueOf(5));
        }
    }

    private static boolean c(Context context) {
        int packageVersionCode = new PackageManagerHelper(context).getPackageVersionCode("com.huawei.appmarket");
        HMSLog.i("UpdateManager", "getHiappVersion is " + packageVersionCode);
        return ((long) packageVersionCode) >= 70203000;
    }
}
